package com.ivan.cmn.controller;

import com.ivan.cmn.service.DictService;
import com.ivan.common.result.Result;
import com.ivan.model.entity.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 * @Author      Ivan
 * @Date        2023/4/2 17:38
 * @Description
 */
@Api(tags = "数据字典管理接口")
//@CrossOrigin//使用gateway后做了配置
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据dictCode查询数据列表")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    @ApiOperation(value = "导出数据字典")
    @GetMapping("/exportDictData")
    public Result exportDictData(HttpServletResponse response) {
        dictService.exportDictData(response);
        return Result.ok();
    }

    @ApiOperation(value = "导入数据字典")
    @PostMapping("/importDictData")
    public Result importDictData(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }

    @ApiOperation(value = "根据dictcode和value查询")
    @GetMapping("/getNameByDictCodeAndValue/{dictCode}/{value}")
    public String getNameByDictCodeAndValue(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value") String value) {
        String name = dictService.getNameByDictCodeAndValue(dictCode, value);
        return name;
    }

    @ApiOperation(value = "根据value查询")
    @GetMapping("/getNameByValue/{value}")
    public String getNameByValue(@PathVariable("value") String value) {
        String name = dictService.getNameByDictCodeAndValue(null, value);
        return name;
    }

}
